# $1:容器id或容器名车 $2: 恢复和备份，1 备份 2 恢复备份文件 $3: 恢复备份文件名称

# 日期
DATE=`date +%Y_%m_%d_%H_%M`
echo "今天是：$DATE,开始执行数据备份"

# 初始化宿主机备份文件目录
if [ ! -d /home/workspace/backup ];then
   echo "新建宿主机backup目录"
   sudo mkdir /home/workspace/backup
   sudo chmod 777 /home/workspace/backup
else 
   echo "宿主机backup目录已存在"
fi
# 进入运行的容器,执行备份命令并将文件保存至宿主机/home/workspace/backup/目录下
if [ "$2" = '1' ];then
   echo "开始执行mongodb备份，文件保存至/home/workspace/backup/$DATE.gz"
   sudo docker exec -it $1  sh -c 'mongodump --archive' > /home/workspace/backup/$DATE.gz
else if [ "$2" = '2' ];then
   echo "开始执行mongodb恢复，文件保存至/home/workspace/backup/$3"
   sudo docker exec -it $1  sh -c 'mongostore --archive' > /home/workspace/backup/$3
fi

exit
